package com.jadesoft.ciadapter;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.jadesoft.ciadapter.library.JdPageHelper;
import com.jadesoft.ciadapter.plugin.LyricPlugin;
import com.jadesoft.ciadapter.plugin.OzhMenuPlugin;
import com.jadesoft.ciadapter.plugin.ScreenMetaPlugin;
import com.jadesoft.ciadapter.plugin.StabPlugin;
import com.jadesoft.ciadapter.plugin.TabMenuPlugin;
import com.jadesoft.ciadapter.plugin.base.PluginManager;

import freemarker.template.TemplateException;

public class JdFrameFilter implements Filter {
	private static Logger logger = Logger.getLogger(JdFrameFilter.class);

	public void destroy() {
		// TODO Auto-generated method stub
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void doFilter(ServletRequest request, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		JdPageHelper helper = JdPageHelper.getHelper(req);

		helper.css_enqueue("central");
		helper.js_enqueue("lib/jquery-1.4.2.min");

		helper.css_enqueue("wp-global", "wp-colors", "wp-admin");
		helper.css_enqueue("wp-mod");
		helper.css_enqueue("menu");
		helper.js_enqueue("wp-utils", "hoverIntent.dev","component/jd-mouse-action-cls");
		helper.js_enqueue("wp-common");

		helper.setPageMeta("current_file", req.getRequestURI());
		
		new LyricPlugin().init(req);
		new ScreenMetaPlugin().init(req);
		new OzhMenuPlugin().init(req);
		new TabMenuPlugin().init(req);
		new StabPlugin().init(req);
		// 自定义的 response
		HttpCharacterResponseWrapper response = new HttpCharacterResponseWrapper((HttpServletResponse) res);
		// 提交给 Servlet 或者下一个 Filter
		chain.doFilter(req, response);
		
		StringWriter menu = new StringWriter();
		try {
			JdPageHelper.loadTemplate("jd_base/menu.ftl", null, menu,req);
		} catch (TemplateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		/* 创建一个数据模型Create a data model */
		Map data = new HashMap();
		data.put("title", helper.getPageMeta("title"));
		data.put("menu", PluginManager.doFilter("menu", req,menu.toString()));
		data.put("javascript", helper.js_export());
		data.put("css", helper.css_export());
		data.put("content", response.getCharArrayWriter().toString());
		data.put("time", System.currentTimeMillis());
		/* 合并数据模型和模版 */
		Writer out = res.getWriter();
		try {
			JdPageHelper.loadTemplate("jd_base/jd_template.ftl", data, out,req);
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.flush();
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		logger.info("JdFrameFilter init");

	}

}
